package com.swpu.service_user.component;

import com.swpu.service_user.util.RsaUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class RSAComponent {

  static private Map<String,Object> keyPair=new LinkedHashMap<>();

    static  {
        Map<String, Object> keyPair_ = null;
        try {
            keyPair_ = RsaUtil.genKeyPair();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        keyPair=keyPair_;
    }


    public static String getRsaPrivateKey() {
        Key key = (Key) keyPair.get("privateKey");
        return Base64.encodeBase64String(key.getEncoded());
    }

   static public String getRsaPublicKey() {
       Key key = (Key) keyPair.get("publicKey");
       return Base64.encodeBase64String(key.getEncoded());

    }
}
